package ru.vsu.cs.pm.pages;

import org.apache.wicket.feedback.ComponentFeedbackMessageFilter;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import ru.vsu.cs.pm.BasePage;
import ru.vsu.cs.pm.PMSession;
import ru.vsu.cs.pm.beans.AccountManager;
import ru.vsu.cs.pm.models.Account;
import ru.vsu.cs.pm.models.AccountModel;
import ru.vsu.cs.pm.models.AccountType;
import ru.vsu.cs.pm.models.User;

/**
 * User: ohodyakov
 * Date: 23.12.2010
 * Time: 10:33:07
 */
public class AccountPage extends BasePage {
    @SpringBean
    private AccountManager accountManager;

    public AccountPage() {
        this(new AccountModel());
    }

    public AccountPage(Account account) {
        this(new AccountModel(account));
    }

    public AccountPage(IModel<Account> accountModel) {
        User user = PMSession.get().getAuthenticatedUser();
        boolean newAccount = (accountModel.getObject().getId() == null);

        add(new Label("accountMessage", newAccount ? "Add new account" : "Edit the account"));
        add(new AccountForm("accountForm", accountModel));
    }

    private class AccountForm extends Form<Account> {

        public AccountForm(String wicketId, IModel<Account> accountModel) {
            super(wicketId, new CompoundPropertyModel<Account>(accountModel));

            boolean isNewAccount = (accountModel.getObject().getId() == null);

            TextField<String> titleField = new TextField<String>("title");
            titleField.setRequired(true);
            add(titleField);

            TextField<String> descriptionField = new TextField<String>("description");
            descriptionField.setRequired(true);
            add(descriptionField);

            TextField<Long> moneyField = new TextField<Long>("money");
            moneyField.setRequired(true);
            moneyField.setOutputMarkupId(true);
            add(moneyField);
            moneyField.setVisible(isNewAccount);

            add(new FeedbackPanel("accountFeedback", new ComponentFeedbackMessageFilter(this)));

            Button cancelBtn = new Button("cancelButton"){
                public void onSubmit() {
                    setRedirect(true);
                    setResponsePage(AccountListPage.class);
                }
            };

            cancelBtn.setDefaultFormProcessing(false);
            add(cancelBtn);
        }

        protected void onSubmit() {
            Account account = getModelObject();
            account.setUser(PMSession.get().getAuthenticatedUser());
            account.setType(AccountType.DEFAULT);
            accountManager.saveOrUpdateAccount(account);

            setRedirect(true);
            setResponsePage(AccountListPage.class);
        }
    }
}
